//
//  RecordSportView.m
//  CoolTennisBall
//
//  Created by CA on 16/3/30.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "RecordSportView.h"
#import "CoollangTennisBall-Swift.h"

static const CGFloat kAnimationDuration = 1.0f;

@interface RecordSportView ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *percentConstraint;
@end

@implementation RecordSportView

#pragma mark - Getter/Setter

- (void)setMaxValue:(NSInteger)maxValue
{
    _maxValue = maxValue;
    self.valueLabel.format = @"%d";
    self.valueLabel.text = @"0";
    self.valueLabel.method = UILabelCountingMethodEaseInOut;
}

- (void)setCurrentValue:(NSInteger)currentValue
{
    _currentValue = currentValue;
    
    [self.valueLabel countFrom:0 to:currentValue withDuration:kAnimationDuration];
    if (self.maxValue != 0) {
        CGFloat percent = MIN(1, (CGFloat)currentValue/self.maxValue);
        [SpringAnimation springWithDuration:kAnimationDuration animations:^{
            self.percentConstraint.constant = self.bounds.size.width * (1 - percent);
            [self layoutIfNeeded];
        }];
    }
}

@end
